<?php /* УТФ-8:fix */

define( 'SPI_IAM_ERRMSG_LOGIN_IS_EMPTY',    'Login_is_empty' );
define( 'SPI_IAM_ERRMSG_USER_NOT_EXIST',    'User_not_exists' );
define( 'SPI_IAM_ERRMSG_ACCESS_DENY',       'Access_deny' );
define( 'SPI_IAM_ERRMSG_NOT_CONFIRMED',     'Not_confirmed' );

class SPIcore__Iam
{
    private $profile_item               = false;
    private $last_login_status          = null;
    private $last_login_message         = null;

    public function reset()
    {
        SPI::I('session')->set_var('Iam::login',  null );
        SPI::I('session')->set_var('Iam::passwd', null );
        SPI::I('session')->set_var('Iam::profile_item', null );

        $this->profile_item = false;

        return true;
    }

    public function login( $in_login = false, $in_passwd = false, $login_by_id = null )
    {
        $in_login  = trim( $in_login );
        $in_passwd = trim( $in_passwd );

        if( $in_login && $in_passwd )
        {
            $this->logout();

            SPI::I('session')->set_var('Iam::login',    $in_login );
            SPI::I('session')->set_var('Iam::passwd',   md5( $in_passwd ) );

            $apply_new_login = true;
        }
        else
        {
            $apply_new_login = false;
        }

        // --------------------------------------------------------------------

        if( $login_by_id !== null )
        {
            $this->logout();

            $item_by_id = SPIm::I('profiles')->get_by_id( $login_by_id );

            SPI::I('session')->set_var('Iam::login',    $item_by_id['email'] );
            SPI::I('session')->set_var('Iam::passwd',   $item_by_id['passwd'] );

            $apply_new_login = true;

            unset( $item_by_id );
        }

        // --------------------------------------------------------------------

        $login  = (string) SPI::I('session')->get_var('Iam::login');
        $passwd = (string) SPI::I('session')->get_var('Iam::passwd');

        if( $login == '' )
        {
            $this->reset();

            $this->last_login_status = false;
            $this->last_login_message = SPI_IAM_ERRMSG_LOGIN_IS_EMPTY;

            return true;
        }

        $profile_item = SPIm::I('profiles')->get_by_login_n_password( $login, $passwd );

        if( $profile_item === false )
        {
            $this->reset();

            $this->last_login_status = false;
            $this->last_login_message = SPI_IAM_ERRMSG_USER_NOT_EXIST;

            return true;
        }

        if( $profile_item['workstatus'] == 2 )
        {
            $this->reset();

            $this->last_login_status = false;
            $this->last_login_message = SPI_IAM_ERRMSG_NOT_CONFIRMED;

            return true;
        }

        if( $profile_item['workstatus'] != 1 )
        {
            $this->reset();

            $this->last_login_status = false;
            $this->last_login_message = SPI_IAM_ERRMSG_ACCESS_DENY;

            return true;
        }

        if( $profile_item['birthday'] )
        {
            $profile_item['birthday__view_converted'] = strftime( '%d.%m.%Y', strtotime( $profile_item['birthday'] ) );
        }
        else
        {
            $profile_item['birthday__view_converted'] = null;
        }

        $this->last_login_status = true;
        $this->last_login_message = null;

        $profile_item['permissions_keys'] = SPIm::I('profiles')->build_permissions_keys( $profile_item['permissions'] );
        $profile_item['acp_allow'] =  $profile_item['permissions_keys'][SPI_IAM_PERM_ADMIN];

        $this->profile_item = $profile_item;

        SPI::I('session')->set_var('Iam::profile_item',$profile_item);

        return true;
    }

    public function refersh_profile_data()
    {
        return $this->login();
    }

    public function logout()
    {
        $this->reset();

        SPI::I('session')->destroy();

        return true;
    }

    public function get_profile( $key = null )
    {
        if( $this->profile_item === false )
        {
            return false;
        }

        if( $key === null )
        {
            return $this->profile_item;
        }

        return $this->profile_item[$key];
    }

    public function get_id()
    {
        if( $this->profile_item === false )
        {
            return 0;
        }

        return (int) $this->profile_item['id'];
    }

    public function is_permit( $checking_perm_id )
    {
        if( $this->profile_item === false )
        {
            return SPI_IAM_PERM_GUEST;
        }

        return $this->profile_item['permissions_keys'][$checking_perm_id] === true;
    }

    public function check_passwd( $check_passwd )
    {
        if( $this->profile_item === false )
        {
            return false;
        }

        return $this->profile_item['passwd'] == md5( $check_passwd );
    }

    public function change_passwd( $new_passwd )
    {
        if( $this->profile_item === false )
        {
            return false;
        }

        SPI::I('session')->set_var('Iam::passwd', md5( $new_passwd ) );
        return true;
    }

    public function get_last_login_status()
    {
        return $this->last_login_status;
    }

    public function get_last_login_message()
    {
        return $this->last_login_message;
    }
}
